package ranab.server.ftp.gui;

import java.io.InputStream;
import java.io.IOException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import javax.swing.text.html.HTMLDocument;

import ranab.io.IoUtils;
import ranab.server.ftp.FtpConfig;

/**
 * Displays <a href="about.html">about.html</a> page.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class FtpAboutPanel extends PluginPanel implements HyperlinkListener {
    
    public final static String ABOUT_PAGE = "ranab/server/ftp/gui/about.html";
    private JEditorPane mjEditorPane = null;    
  
    /**
     * Constructor.
     */
    public FtpAboutPanel(FtpTree tree) {
        super(tree);
        initComponents();
    }
    
    
    /**
     * Initialize GUI components.
     */
    private void initComponents() {
        setLayout(new BorderLayout());
        mjEditorPane = new JEditorPane();
        mjEditorPane.setEditable(false);
        mjEditorPane.setContentType("text/html");
        goHome();
        mjEditorPane.addHyperlinkListener(this);
        
        JScrollPane editorScrollPane = new JScrollPane(mjEditorPane);
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(480, 340));
        add(editorScrollPane, BorderLayout.CENTER);
       
        JPanel bottomPane = new JPanel();
        bottomPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton homeButton = new JButton("Home");
        homeButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                goHome();
             }
        });
        bottomPane.add(homeButton);
        add(bottomPane, BorderLayout.SOUTH);
    }
    
    /**
     * Handle user mouse click.
     */
    public void hyperlinkUpdate(HyperlinkEvent e) {
        try {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane pane = (JEditorPane) e.getSource();
                if (e instanceof HTMLFrameHyperlinkEvent) {
                    HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
                    HTMLDocument doc = (HTMLDocument)pane.getDocument();
                    doc.processHTMLFrameHyperlinkEvent(evt);
                } 
                else {
                    pane.setPage(e.getURL());
                }
            }
        }
        catch(Throwable th) {
        }
    }
    
    /**
     * Display about page
     */
    private void goHome() {
       InputStream is = null;
        try {
            mjEditorPane.setContentType("text/html");
            is = getClass().getClassLoader().getResourceAsStream(ABOUT_PAGE);
            if (is != null) {
                mjEditorPane.read(is, null);
            }
        }
        catch(IOException ex) {
        }
        finally {
            IoUtils.close(is);
        }
    }   

    /**
     * Reload new configuration - does nothing
     */ 
    public void refresh(FtpConfig config) {
    }
    
    /**
     * Is displayable in the root pane.
     */
    public boolean isDisplayable() {
        return true;
    } 
    
}
